package cn.gjxblog.controller;


import cn.gjxblog.common.model.HttpResult;
import cn.gjxblog.model.CommonConstants;
import cn.gjxblog.model.dto.HouseQueryDTO;
import cn.gjxblog.model.entity.*;
import cn.gjxblog.model.vo.HouseVO;

import cn.gjxblog.service.HouseService;
import cn.gjxblog.service.HouseUserService;
import cn.gjxblog.service.RecommendService;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author gjx
 * @version v1.0
 * @Description
 * @date 2018/12/12 14:33
 */
@RequestMapping("/house")
@RestController
public class HouseController {

    @Resource
    private HouseService houseService;

    @Resource
    private RecommendService recommendService;

    @Resource
    private HouseUserService houseUserService;

    @PostMapping("/searchHouses")
    public HttpResult<PageInfo<HouseVO>> searchHouses(@RequestBody HouseQueryDTO houseQueryDTO ){
//        PageCondition pageCondition = (PageCondition)maps.get("pageCondition");
        PageInfo housePageInfo = houseService.searchHouses(houseQueryDTO);

        ArrayList<HouseVO> houseVOS = Lists.newArrayList();
        List<House> list = housePageInfo.getList();
        for (House house : list) {
            if(house !=null){
                HouseVO houseVO = houseToVO(house);
                houseVOS.add(houseVO);
            }
        }
        housePageInfo.setList(houseVOS);
        return HttpResult.success(housePageInfo);
    }



    @RequestMapping("/getHouse")
    public HttpResult<HouseVO> getHouse(@RequestParam("id")String houseId){
        House house = houseService.getHouse(houseId);
        HouseVO houseVO = houseToVO(house);
        recommendService.increaseHot(houseId);
        HouseUser houseUser = houseUserService.getByHouseId(houseId);
        houseVO.setUserId(houseUser.getUserId());

        return HttpResult.success(houseVO);
    }

    @PostMapping("/queryHousesByUserId")
    public HttpResult<PageInfo<HouseVO>> queryHousesByUserId(@RequestBody HouseQueryDTO houseQueryDTO){
        PageInfo housePageInfo = houseService.queryHousesByUserId(houseQueryDTO);
        List<House> list = housePageInfo.getList();

        List<HouseVO> houseVOs = list.stream()
                .map(house -> houseToVO(house))
                .collect(Collectors.toList());

        housePageInfo.setList(houseVOs);
        return HttpResult.success(housePageInfo);
    }



    @GetMapping("/queryCommunitys")
    public HttpResult<List<Community>> queryCommunitys(){
        List<Community> list = houseService.queryCommunitys();
        return HttpResult.success(list);
    }

    @GetMapping("/queryCitys")
    public HttpResult<List<City>> queryCitys(){
        List<City> list = houseService.queryCitys();
        return HttpResult.success(list);
    }

    /**
     * 房产新增
     * @param house
     * @return
     */
    @PostMapping("/addHouse")
    public HttpResult addHouse(@RequestBody House house,@RequestParam("userId") String userId){
        houseService.addHouse(house,userId);
        return HttpResult.success();
    }

    @GetMapping("/queryHotHouses")
    public HttpResult<List<HouseVO>> queryHotHouses(Integer size){
        List<House> hotHouse = recommendService.queryHotHouses(size);
        List<HouseVO> houseVOS = hotHouse.stream().map(this::houseToVO).collect(Collectors.toList());
        return HttpResult.success(houseVOS);
    }

    private HouseVO houseToVO(House house) {
        HouseVO houseVO = new HouseVO();
        try {
            BeanUtils.copyProperties(houseVO,house);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(house.getImages())){
            Splitter on = Splitter.on(",");
            List<String> imglist = on.splitToList(house.getImages());
            if(imglist.size()>0){
                houseVO.setImageList(imglist);
                houseVO.setFirstImg(imglist.get(0));
            }
        }
        if(StringUtils.isNotBlank(house.getFloorPlan())){
            List<String> floorlist = Splitter.on(",").splitToList(house.getFloorPlan());
            if(floorlist.size()>0){
                houseVO.setFloorPlanList(floorlist);
            }
        }

        if(StringUtils.isNotBlank(house.getProperties())){
            List<String> toList = Splitter.on(",").splitToList(house.getProperties());
            if(toList.size()>0){
                houseVO.setFeatureList(toList);
            }
        }

        return houseVO;
    }


    @PostMapping("/queryLastestHouses")
    public HttpResult<List<HouseVO>> queryLastestHouses(){
        List<House> lastest = recommendService.queryLastestHouses();
        List<HouseVO> collect = lastest.stream().
                map(house -> houseToVO(house)).
                collect(Collectors.toList());
        return HttpResult.success(collect);
    }

    @PostMapping("/queryGoodHouses")
    public HttpResult<List<HouseVO>> queryGoodHouses(){
        List<House> lastest = recommendService.queryGoodHouses();
        List<HouseVO> collect = lastest.stream().
                map(house -> houseToVO(house)).
                collect(Collectors.toList());
        return HttpResult.success(collect);
    }

    @PostMapping("/addHouseMsg")
    public HttpResult<Object> addHouseMsg(@RequestBody HouseMsg houseMsg){
        houseService.addHouseMsg(houseMsg);
        return HttpResult.success();
    }


    @GetMapping("/houseRate")
    public HttpResult<Object> houseRate(Double rating,String houseId){
        houseService.updateRating(houseId,rating);
        return HttpResult.success();
    }

    /**
     *  收藏房屋
     * @param houseId
     * @param userId
     * @param b 如果b为true 表示收藏，否则为取消收藏
     * @return
     */
    @GetMapping("/collectHouse")
    public HttpResult<Object> collectHouse(String houseId, String userId, boolean b){
        if(b){
            houseService.addUser2House(houseId,userId,CommonConstants.COLLECT_STATE);
        }else{
            houseService.deleteUser2House(houseId,userId,CommonConstants.COLLECT_STATE);
        }
        return HttpResult.success();
    }


    @GetMapping("/deleteHouse")
    public HttpResult deleteHouse(String houseId){
        houseService.deleteHouse(houseId);
        return HttpResult.success();
    }
}
